package com.xxx.meter.ui.meter.reading.info;

import android.content.pm.ActivityInfo;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xxx.meter.R;
import com.xxx.meter.base.BaseActivity;
import com.xxx.meter.model.data.meter.MeterUtil;
import com.xxx.meter.model.http.bean.MachineInfoBean;
import com.xxx.meter.model.utils.ButtonUtil;
import com.xxx.meter.model.utils.ToastUtil;
import com.xxx.meter.ui.meter.reading.selection.MeterReadingSelectionWindow;
import com.xxx.meter.ui.meter.reading.selection2.MeterReadingSelectionWindow2;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MeterReadingInfoActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.main_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.main_refresh)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.not_data)
    ImageView mNotData;
    @BindView(R.id.main_title)
    TextView mTitle;

    private String machineId;
    private String machineType;
    private MeterReadingInfoAdapter mAdapter;
    private ArrayList<MachineInfoBean> mList = new ArrayList<>();
    private MeterReadingSelectionWindow2 mSelectionDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_machine_info;
    }

    @Override
    protected void initData() {
        //禁止横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        machineId = getIntent().getStringExtra("machineId");
        machineType = getIntent().getStringExtra("machineType");
        String machineName = getIntent().getStringExtra("machineName");

        mTitle.setText(machineName);
        mAdapter = new MeterReadingInfoAdapter(mList);
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecycler.setAdapter(mAdapter);
        mRefresh.setOnRefreshListener(this);
        mAdapter.setOnItemClickListener(this);

        loadInfo();
    }

    @OnClick({R.id.machine_info_submit, R.id.main_return, R.id.not_data, R.id.not_net})
    public void OnClick(View view) {
        if (ButtonUtil.isFastClick()) return;
        switch (view.getId()) {
            case R.id.machine_info_submit:
                submit();
                break;
            case R.id.main_return:
                finish();
                break;
            case R.id.not_data:
                loadInfo();
                break;
            case R.id.not_net:
                loadInfo();
                break;
        }
    }

    @Override
    public void onRefresh() {
        //刷着玩
        mRefresh.setRefreshing(false);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
        if (ButtonUtil.isFastClick()) return;

        final MachineInfoBean machineInfoBean = mList.get(position);
        if (machineInfoBean.getItemType() == MachineInfoBean.TEXT) {
            mSelectionDialog = MeterReadingSelectionWindow2.getInstance(this, machineInfoBean.getData(), new MeterReadingSelectionWindow2.Callback() {
                @Override
                public void callback(double value) {
                    machineInfoBean.setData(value);
                    mList.set(position, machineInfoBean);
                    mAdapter.notifyItemChanged(position);
                    mSelectionDialog.dismiss();
                }
            });
            mSelectionDialog.show();
        }
    }

    /**
     * @Data 加载本地机器信息
     */
    private void loadInfo() {
        List<MachineInfoBean> machineInfoList = MeterUtil.getMachineInfoList(machineType, machineId);
        if (machineInfoList.size() == 0) {
            mRefresh.setVisibility(View.GONE);
            mNotData.setVisibility(View.VISIBLE);
        } else {
            mRefresh.setVisibility(View.VISIBLE);
            mNotData.setVisibility(View.GONE);

            mList.clear();
            mList.addAll(machineInfoList);
            mAdapter.notifyDataSetChanged();
            mRefresh.setRefreshing(false);
        }
    }

    /**
     * @Data 提交本地修改
     */
    private void submit() {
        showLoading();
        new Thread(new Runnable() {
            @Override
            public void run() {
                MeterUtil.updateMachineInfo(machineType, machineId, mAdapter.getData());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        hideLading();
                        ToastUtil.showToast("提交成功");
                        finish();
                    }
                });
            }
        }).start();
    }
}
